import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DialogUtil {
  DialogUtil._();

  static void show({
    required BuildContext context,
    required Widget child,
    bool barrierDismissible = true,
    Function? onDismiss,
  }) {
    Widget dialog = Dialog(
      backgroundColor: Colors.transparent,
      insetPadding: EdgeInsets.all(20),
      child: child,
    );
    if (!barrierDismissible) {
      dialog = WillPopScope(
        child: dialog,
        onWillPop: () async {
          return false;
        },
      );
    }
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (context) => dialog,
    ).then((value) {
      onDismiss?.call();
    });
  }

  static void showBottom({
    required BuildContext context,
    double? topRadius,
    required Widget child,
  }) {
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.white,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(topRadius ?? 10.w),
        ),
      ),
      builder: (context) {
        Widget root = Padding(
          padding: EdgeInsets.only(
            bottom: MediaQuery.of(context).viewInsets.bottom, // 适配键盘
          ),
          child: child,
        );
        return SafeArea(child: root);
      },
    );
  }

  static void hide(BuildContext context) {
    if (Navigator.of(context).canPop()) {
      Navigator.of(context).pop();
    }
  }
}
